package com.xjl.controller;


import com.xjl.domain.Myweathers;
import com.xjl.domain.ResponseResult;
import com.xjl.mapper.MyWeatherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@RestController
public class TestController {
    @GetMapping("/test")
    public String test(){
        return "test";
    }


    @RequestMapping("/getRequestAndResponse")
    public ResponseResult getRequestAndResponse(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println(request);
        return new ResponseResult(200,"成功");
    }

    @Autowired
    private MyWeatherMapper myWeatherMapper;
    @PostMapping("/get")
    public void get(@RequestBody Myweathers myweathers){

        myweathers.setPressure(String.valueOf(Double.valueOf(myweathers.getPressure())*10));
        if (myweathers.getWindDirection().equals("0")){
            myweathers.setWindDirection("南风");
        }else if(myweathers.getWindDirection().equals("90")){
            myweathers.setWindDirection("西风");
        } else if (myweathers.getWindDirection().equals("180")){
            myweathers.setWindDirection("北风");
        } else if (myweathers.getWindDirection().equals("270")){
            myweathers.setWindDirection("东风");
        }else if (myweathers.getWindDirection().equals("45")){
            myweathers.setWindDirection("西南风");
        } else if (myweathers.getWindDirection().equals("125")){
            myweathers.setWindDirection("西北风");
        } else if (myweathers.getWindDirection().equals("225")){
            myweathers.setWindDirection("东北风");
        } else if (myweathers.getWindDirection().equals("315")){
            myweathers.setWindDirection("东南风");
        }

        String time=myweathers.getDetectedTime();
        LocalDateTime dateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String deviceTime = dateTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
        myweathers.setDetectedTime(deviceTime);
        Date now = new Date();

        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMdd");
        String formattedDateTime2 = formatter2.format(now);

        String time0 = formattedDateTime2+"1112";
        String time0_1 = formattedDateTime2+"1113";
        String time1 = formattedDateTime2+"0100";
        String time2 = formattedDateTime2+"0200";
        String time3 = formattedDateTime2+"0300";
        String time4 = formattedDateTime2+"0400";
        String time5 = formattedDateTime2+"0500";
        String time6 = formattedDateTime2+"0600";
        String time7 = formattedDateTime2+"0700";
        String time8 = formattedDateTime2+"0800";
        String time9 = formattedDateTime2+"0900";
        String time10 = formattedDateTime2+"1000";
        String time11 = formattedDateTime2+"1100";
        String time12 = formattedDateTime2+"1200";
        String time13 = formattedDateTime2+"1300";
        String time14 = formattedDateTime2+"1400";
        String time15 = formattedDateTime2+"1500";
        String time16 = formattedDateTime2+"1600";
        String time17 = formattedDateTime2+"1700";
        String time18 = formattedDateTime2+"1800";
        String time19 = formattedDateTime2+"1900";
        String time20 = formattedDateTime2+"2000";
        String time21 = formattedDateTime2+"2100";
        String time22 = formattedDateTime2+"2200";
        String time23 = formattedDateTime2+"2300";
        Myweathers myweathersFromSql=null;

        myweathers.setWindSpeed((myweathers.getWindSpeed()+"m/s"));
        myweathers.setAmbientTemperature((myweathers.getAmbientTemperature()+"°C"));
        myweathers.setAmbientHumidity((myweathers.getAmbientHumidity()+"%"));
        myweathers.setPressure((myweathers.getPressure()+"kpa"));
        myweathers.setRainfall((myweathers.getRainfall()+"mm"));
        if(time0.equals(deviceTime)||time0_1.equals(deviceTime)){
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time0);
            if (myweathersFromSql!=null){
                System.out.println("0点数据");
            }else {
                myweathers.setDetectedTime(time0);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.println("0点数据");
            }
        } else if (time1.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("1点数据");
        }else if (time2.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("2点数据");
        }else if (time3.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("3点数据");
        }else if (time4.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("4点数据");
        }else if (time5.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("5点数据");
        }else if (time6.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("6点数据");
        }else if (time7.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("7点数据");
        }else if (time8.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("8点数据");
        }else if (time9.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("9点数据");
        }else if (time10.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("10点数据");
        }else if (time11.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("11点数据");
        }else if (time12.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("12点数据");
        }else if (time13.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("13点数据");
        }else if (time14.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("14点数据");
        }else if (time15.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("15点数据");
        }else if (time16.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("16点数据");
        }else if (time17.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("17点数据");
        }else if (time18.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("18点数据");
        }else if (time19.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("19点数据");
        }else if (time20.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("20点数据");
        }else if (time21.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("21点数据");
        }else if (time22.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("22点数据");
        }else if (time23.equals(deviceTime)) {
            myWeatherMapper.insertWeatherStation(myweathers);
            System.out.println("23点数据");
        }

        System.out.println(myweathers);
    }
}
